package com.dlc.felear.xiaoensale.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.entity.Indicator;
import com.dlc.felear.xiaoensale.helper.JsonHelper;

import java.util.ArrayList;
import java.util.List;


public class PageIndicatorAdapter extends BaseAdapter {

    private static final String TAG = "PageIndicatorAdapter";
    // 上下文
    private Context context;
    // 数据
    private List<Indicator> items;

    public PageIndicatorAdapter(Context context) {
        this.context = context;
        items = new ArrayList<>();
    }

    public List<Indicator> getData() {
        return items;
    }


    public void setItems(int pageSize,int index) {
        Log.e(TAG,"pageSize="+pageSize+" pageIndex="+index);
        clearData();
        ArrayList<Indicator> temp=new ArrayList();
        for (int i = 0; i < pageSize; i++) {
            if(index==i){
                temp.add(new Indicator(Integer.toString(i+1),true));
            }else {
                temp.add(new Indicator(Integer.toString(i+1),false));
            }
        }
        items.addAll(temp);
        Log.e(TAG,""+ JsonHelper.toJson(items));
        notifyDataSetChanged();
    }

    public void clearData() {
        items.clear();
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return items == null ? 0 : items.size();
    }

    @Override
    public Indicator getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
//            convertView=LayoutInflater.from(context).inflate(R.layout.adapter_indicator_layout,parent,false);
            convertView = View.inflate(context, R.layout.adapter_indicator_layout, null);
            holder.tv_name = convertView.findViewById(R.id.tv_goods_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Indicator indicator=getItem(position);
        holder.tv_name.setText(indicator.getName());
        if(indicator.isSelect()){
            holder.tv_name.setTextColor(0xff000000);
            holder.tv_name.setBackgroundResource(R.drawable.tv_indicator_1_shape);
        }else {
            holder.tv_name.setTextColor(0xffffffff);
            holder.tv_name.setBackgroundResource(R.drawable.tv_indicator_2_shape);
        }
        return convertView;
    }





    static class ViewHolder {
        TextView tv_name;
    }

}